package net.brucecooper.mindmap.client;

import gwt.canvas.client.Canvas;

public class HorizontalPanel extends Panel {
	
	public HorizontalPanel() {
		super();
	}

	public HorizontalPanel(Panel.Alignment alignment) {
		super(alignment);
	}
	

	@Override
    public void layout(Canvas canvas) {
		int x = 0;
		int h = getPreferredHeight(canvas);

		if (children != null) {
			for (int i = 0; i < childCount; i++) {
				GraphicComponent comp = children[i];
				comp.layout(canvas);
				int ch = comp.getPreferredHeight(canvas);
				int cw = comp.getPreferredWidth(canvas);

				int y = 0;
		        switch (alignment) {
		        case TOP:
		            y =  0;
		            break;
		        case CENTER:
		            y = (h - ch) / 2;
		            break;
		        case BOTTOM:
		            y = h - ch;
		            break;
		        }
				comp.setBounds(x, y, cw, ch);
				x += cw + spacing;
			}
		}
	}

	@Override
    public int getPreferredHeight(Canvas canvas) {
		if (children != null) {
			int max = 0;
			for (int i = 0; i < childCount; i++) {
				int h = children[i].getPreferredHeight(canvas);
				if (h > max)
					max = h;
			}
			return max;
		} else {
			return 0;
		}
	}

	@Override
    public int getPreferredWidth(Canvas canvas) {
		if (children != null) {
			int width = 0;
			for (int i = 0; i < childCount; i++) {
				GraphicComponent comp = children[i];
				width += comp.getPreferredWidth(canvas);
			}
			width += spacing * (childCount-1);
			return width;
		} else {
			return 0;
		}
	}

}
